using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.AI;

public class SkillManager : singleTon<SkillManager>
{
    public Skill_List SkillListSaber;
    public Skill_List SkillListArcher;
    public Skill_List SkillListMage;
    private Animator anim;
    public ParticleSystem effect;
    public ParticleSystem newEffect;
    private RaycastHit hitInfo;
    public GameObject Pointer;
    private GameObject newPointer;
    private bool isUsingSkill;
    private int range = 5;
    private SkillSlotHolder[] SkillSlotHolders;

    public List<bool> HaveCD = new List<bool>();
    public List<float> CD = new List<float>();
    public List<float> CDtemp = new List<float>();
    private float timeCaluate = 0;
    private int Skill4Flag = -1;

    
    private void Start()
    {
        SkillSlotHolders = InventoryManager.Instance.skillUI.slotHolders;
        for (int i = 0; i < 4; i++)
        {
            CD.Add(SkillListSaber.items[i].coolDown);
        }
        for (int i = 0; i < 4; i++)
        {
            CD.Add(SkillListArcher.items[i].coolDown);
        }
        for (int i = 0; i < 4; i++)
        {
            CD.Add(SkillListMage.items[i].coolDown);
        }

        for (int i = 0; i < 12; i++)
        {
            CDtemp.Add(CD[i]);
        }
        
    }

    private void Update()
    {
        AnimationTrigger();
        EffectTrriger();
        PersonalizedDesigne();
        CalculateCD();
    }
    protected override void Awake()
    {
        base.Awake();
    }


    public void AnimationTrigger()
    {
        if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
        {
            anim = GameManager.Instance.playerStats.gameObject.GetComponent<Animator>();
            if (Input.GetKeyDown(KeyCode.Q) && !HaveCD[0])
            {
                anim.SetTrigger("Skill1");
            }

            if (Input.GetKeyDown(KeyCode.E) && !HaveCD[1])
            {
                anim.SetTrigger("Skill2");
            }

            if (Input.GetKeyDown(KeyCode.R) && !HaveCD[2])
            {
                anim.SetTrigger("Skill3");
            }

            if (Input.GetKeyDown(KeyCode.T) && !HaveCD[3])
            {
                anim.SetBool("SSkill4", true);
                var FireEquips = GameObject.FindGameObjectsWithTag("CanFire");
                foreach (var FireEquip in FireEquips)
                {
                    var currentInstance = Instantiate(SkillManager.Instance.SkillListSaber.items[3].skillEffect,
                        FireEquip.transform);
                    var psUpdater = currentInstance.GetComponent<PSMeshRendererUpdater>();
                    psUpdater.UpdateMeshEffect(FireEquip);
                    Destroy(currentInstance,6f);
                }
            }
        }
        else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
        {
            anim = GameManager.Instance.playerStats.gameObject.GetComponent<Animator>();
            if (Input.GetKeyDown(KeyCode.Q) && !HaveCD[4])
            {
                anim.SetTrigger("Skill1");
            }
            if (Input.GetKeyDown(KeyCode.E) && !HaveCD[5])
            {
                anim.SetTrigger("Skill2");
            }
            if (Input.GetKeyDown(KeyCode.R) && !HaveCD[6])
            {
                anim.SetTrigger("Skill3");
            }
            if (Input.GetKeyDown(KeyCode.T) && !HaveCD[7])
            {
                anim.SetTrigger("Skill4");
            }
        }
        else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
        {
            anim = GameManager.Instance.playerStats.gameObject.GetComponent<Animator>();
            if (Input.GetKeyDown(KeyCode.Q) && !HaveCD[8])
            {
                anim.SetTrigger("Skill1");
            }
            if (Input.GetKeyDown(KeyCode.E) && !HaveCD[9])
            {
                anim.SetTrigger("Skill2");
            }
            if (Input.GetKeyDown(KeyCode.R) && !HaveCD[10])
            {
                anim.SetTrigger("Skill3");
            }
            if (Input.GetKeyDown(KeyCode.T) && !HaveCD[11])
            {
                anim.SetTrigger("Skill4");
            }
        }
    }

    public void EffectTrriger()
    {
        if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
        {
            

            if (Input.GetKeyDown(KeyCode.T) && !HaveCD[3])
            {
                newEffect = Instantiate(SkillListSaber.items[3].skillParticle,
                    GameManager.Instance.playerStats.gameObject.transform.position, transform.rotation);
                GameManager.Instance.playerStats.gameObject.transform
                    .DOLocalRotate(new Vector3(0f, 360 * 30.0f, 0f), 6.0f, RotateMode.FastBeyond360).SetEase(Ease.Linear);
                Skill4Flag = 0;
                HaveCD[3] = true;
            }
        }
        
        else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
        {
            
        }
        
        else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
        {
            
        }

        if (newEffect != null)
        {
            newEffect.transform.position = anim.gameObject.transform.position;
            if (newEffect.isStopped)
            {
                Destroy(newEffect.gameObject);
            }
        }
    }

    public void PersonalizedDesigne()
    {
        if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
        {
            if (Skill4Flag != -1)
            {
                if (SkillListSaber.items[3].skillType == SkillType.AoeAttack)
                {
                    if (timeCaluate >= 0.5)
                    {
                        if (Skill4Flag <= 10)
                        {
                            CalucateDamage(GameManager.Instance.playerStats.gameObject.transform.position,
                                (int)(GameManager.Instance.playerStats.attackData.maxDamage*0.8f), SkillListSaber.items[3].AoeRange);
                        }

                        Skill4Flag += 1;
                        timeCaluate = 0;
                    }

                    timeCaluate += Time.deltaTime;
                    if (Skill4Flag == 12)
                    {
                        anim.SetBool("SSkill4", false);
                        anim.SetTrigger("Dazzy");
                        Skill4Flag = -1;
                    }

                }
            }
        }

        else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
        {
            
        }
        else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
        {
           
        }
    }

    public void CalucateDamage(Vector3 pos,int damage,float range)
    {
        var colliders = Physics.OverlapSphere(pos,range );
        foreach (var collider in colliders)
        {
            if (collider.CompareTag("Enemy"))
            {
                var targetStats=collider.gameObject.GetComponent<CharacterStats>();
                targetStats.TakeDamage(damage, targetStats);
            }
        }
    }
    
    public void CalucateSlow(Vector3 pos,float range)
    {
        var colliders = Physics.OverlapSphere(pos,range );
        foreach (var collider in colliders)
        {
            if (collider.CompareTag("Enemy"))
            {
                collider.gameObject.GetComponent<NavMeshAgent>().speed -=(collider.gameObject.GetComponent<NavMeshAgent>().speed)/10;
            }
        }
    }
    
    

    public void CalculateCD()
    {
        // if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
        // {
        //     for (int i = 0; i < SkillSlotHoldersSaber.Length; i++)
        //     {
        //         if (HaveCD[i])
        //         {
        //             SkillSlotHoldersSaber[i].skillUI.StartMask(i);
        //         }
        //     }
        // }
        // else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
        // {
        //     for (int i = 4; i < 8; i++)
        //     {
        //         if (HaveCD[i])
        //         {
        //             SkillSlotHoldersSaber[i%4].skillUI.StartMask(i%4);
        //         }
        //     }
        // }
        // else if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
        // {
        //     for (int i = 8; i < 12; i++)
        //     {
        //         if (HaveCD[i])
        //         {
        //             SkillSlotHoldersSaber[i%4].skillUI.StartMask(i);
        //         }
        //     }
        // }

        for (int i = 0; i < 12; i++)
        {
            if (HaveCD[i])
            {
                if (CDtemp[i] <= 0)
                {
                    HaveCD[i] = false;
                    CDtemp[i] = CD[i];
                }
                CDtemp[i] -= Time.deltaTime;
            }
        }
    }

    public bool CheckRange(int i,RaycastHit hitInfo)
    {
        if (Vector3.Distance(hitInfo.point, GameManager.Instance.playerStats.transform.position) >
            SkillListSaber.items[i].skillRange)
        {
            return false;
        }
        return true;
    }

    
    
    
    
    
}
